package cn.broadsense.common.utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.Method;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * @author: pengxin
 * @create: 2024-07-26 10:34
 * @Version 1.0
 **/
public class Sm4SecurityUtil {

    /**
     * 加密
     *
     * @param data
     * @return
     */
    public static String encrypt(String data) {
        HttpRequest request = new HttpRequest("http://10.1.1.31:8087/server/service/hnymSM4Encrypt");
        request.setMethod(Method.POST);
        request.form("data", data);
        request.form("dataType", 1);
        request.form("rgisterIden", "185fec9f323f4692b6f72695559e5700");
        String body = request.execute().body();
        JSONObject object = JSONUtil.parseObj(body);
        return object.getStr("data");
    }


    /**
     * 解密
     *
     * @param sealData
     * @return
     */
    public static String decrypt(String sealData) {
        HttpRequest request = new HttpRequest("http://10.1.1.31:8087/server/service/hnymSM4Decrypt");
        request.setMethod(Method.POST);
        request.form("sealData", sealData);
        request.form("dataType", 1);
        request.form("rgisterIden", "185fec9f323f4692b6f72695559e5700");
        String body = request.execute().body();
        JSONObject object = JSONUtil.parseObj(body);
        return object.getStr("data");
    }


    /**
     * 解密
     *
     * @param sealData
     * @return
     */
    public static String decrypt2(String sealData) {

        return  Base64.decodeStr(sealData);
    }
}
